package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.EmailUtil;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取表单数据
        String deptId = request.getParameter("deptId");
        String patientName = (String) request.getSession().getAttribute("user");

        // 注册病人（这是一个占位符，请实现您的注册逻辑）
        boolean registrationSuccess = registerPatient(deptId, patientName);

        if (registrationSuccess) {
            // 使用静态电子邮件地址
            String doctorEmail = "2790825892@qq.com";

            // 发送邮件通知
            String subject = "新病人注册通知";
            String body = "有一位新病人注册了您的科室，请登录系统查看详情。";
            EmailUtil.sendEmail(doctorEmail, subject, body);

            // 重定向到成功页面或显示成功消息
            response.sendRedirect("success.jsp");
        } else {
            // 处理注册失败
            request.setAttribute("error", "注册失败，请重试。");
            request.getRequestDispatcher("homePatient.jsp").forward(request, response);
        }
    }

    private boolean registerPatient(String deptId, String patientName) {
        // 实现您的注册逻辑
        return true; // 如果注册成功，返回true
    }
}